package com.isoft.demo01.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.demo01.bean.BaseController;
import com.isoft.demo01.bean.ResponseData;
import com.isoft.demo01.bean.ResultCode;
import com.isoft.demo01.entity.Order;
import com.isoft.demo01.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sys/order")
@CrossOrigin
public class OrderController extends BaseController {
    @Autowired
    OrderService orderServiceSys;

    @GetMapping("{orderid}")
  public List<Order> find(@PathVariable("orderid") Integer orderid){
      return orderServiceSys.find(orderid);
  }
    @PostMapping
    public ResponseData add(Order order) {
        boolean r=false;
        r= orderServiceSys.add(order);
        return getResponseData(r);
    }
    @DeleteMapping("{id}")
    public ResponseData delById(@PathVariable Integer id){
        boolean r= orderServiceSys.delById(id);
        return getResponseData(r);
    }
    @DeleteMapping
    public ResponseData delByIds(Integer[] id){
        boolean r= orderServiceSys.delByIds(id);
        return getResponseData(r);
    }
    @GetMapping("{id}")
    public ResponseData getById(@PathVariable("id") Integer id) {
        Order order = orderServiceSys.getById(id) ;
        ResponseData r =  new ResponseData() ;
        r.setErrorCode(order != null ? ResultCode.SUCCESS.getCode() : ResultCode.COMMON_FAIL.getCode());
        r.setMsg(order != null ? ResultCode.SUCCESS.getMessage() : ResultCode.COMMON_FAIL.getMessage());
        r.setData(order);
        return r ;
    }

    @GetMapping
    public Map<String , Object> page(Integer curr , Integer limit , Integer orderid, Integer allprice, String ordertime) {
        Page<Order> pageInfo = orderServiceSys.getMore(curr , limit , orderid, ordertime,allprice) ;
        Map<String , Object> map = new HashMap<>() ;
        if(null != pageInfo.getRecords() && pageInfo.getRecords().size() > 0) {
            map.put("code" , 0) ;
            map.put("msg" , "查找成功!") ;
        } else {
            map.put("code" , 1) ;
            map.put("msg" , "查无数据!") ;
        }
        map.put("count" , pageInfo.getTotal()) ;
        map.put("data" ,pageInfo.getRecords() ) ;
        return map ;
    }

    @PutMapping("{id}")
    public ResponseData update(@PathVariable("id") Integer id , Order order) {
        order.setId(id);
        boolean r = orderServiceSys.update(order) ;
        return getResponseData(r) ;
    }

}
